package org.dyk.mallmanagementsystem.controller;

import org.dyk.mallmanagementsystem.model.User;
import org.dyk.mallmanagementsystem.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import java.util.List;

@Controller
@RequestMapping(value = {"/user"})
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户管理页面
     * @param model
     * @return
     */
    @RequestMapping("")
    public String user(@RequestParam(required = true, defaultValue = "0") int page,
                       @RequestParam(required = true, defaultValue = "10") int size,
                       Model model){
        //分页大小
        Pageable pageable = PageRequest.of(page,size);
        //获取分页包
        Page<User> all = userService.findAll(pageable);
        //将分页包返回前端
        model.addAttribute("pageAll",all);
        return "user";
    }

    /**
     * 添加用户 页面
     * @return
     */
    @RequestMapping(value = {"/insertUser"})
    public String insertUser(){
        return "insertUser";
    }

    /**
     * 添加用户 验证
     * @param userLoginName
     * @param userPasswor
     * @param userName
     * @return
     */
    @RequestMapping(value = {"/insert"})
    @ResponseBody
    public boolean insert(@RequestParam String userLoginName,
                          @RequestParam String userPasswor,
                          @RequestParam String userName){
        boolean inser = userService.inser(userLoginName, userPasswor, userName);
        if (inser){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 修改用户 页面
     * @param uid
     * @param model
     * @return
     */
    @RequestMapping(value = {"/updataUser"})
    public String updataUser(@RequestParam int uid, Model model){
        User byUid = userService.findByUid(uid);
        model.addAttribute("user",byUid);
        return "updataUser";
    }

    /**
     * 修改用户 验证
     * @param uid
     * @param userLoginName
     * @param userPasswor
     * @param userName
     * @return
     */
    @RequestMapping(value = {"/updata"})
    @ResponseBody
    public boolean updata(@RequestParam int uid,
                          @RequestParam String userLoginName,
                          @RequestParam String userPasswor,
                          @RequestParam String userName){
        boolean updata = userService.updata(uid, userLoginName, userPasswor, userName);
        if (updata){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 删除用户
     * @param uid
     * @param modelMap
     * @return
     */
    @RequestMapping(value = {"/delete"})
    public String delete(int uid, RedirectAttributesModelMap modelMap){
        boolean delet = userService.delete(uid);
        if (delet){
            modelMap.addFlashAttribute("delMsg","删除成功");
            return "redirect:/user";
        }else {
            modelMap.addFlashAttribute("delMsg","删除失败，该用户有未完成订单");
            return "redirect:/user";
        }
    }

    /**
     * 异步获取所有用户信息
     * @return
     */
    @RequestMapping(value = {"/getAllUser"}, method = RequestMethod.POST)
    @ResponseBody
    public List<User> getAllUser(){
        return userService.findAll();
    }

    /**
     * 自定义查询用户
     * @param loginName
     * @param userName
     * @return
     */
    @RequestMapping(value = {"/selectUser"}, method = RequestMethod.POST)
    @ResponseBody
    public List<User> selectUser(@RequestParam String loginName,
                                 @RequestParam String userName){
        return userService.selectUser(loginName,userName);
    }
}
